<?php

/**
 * Description of Download
 *
 * @author Alisson Chiquitto<chiquitto@chiquitto.com.br>
 */
class ZFKiller_Controller_Action_Helper_Download extends Zend_Controller_Action_Helper_Abstract
{

    /**
     *
     * @param string $content Local do arquivo
     * @param string $filename Nome do arquivo
     * @param string $typeContent Tipo do conteudo da variavel $content. Se igual a file, entao a funcao entendera que e o endereco de um arquivo
     * @see http://davidwalsh.name/php-force-download
     */
    public function download($content, $filename, $typeContent = 'file')
    {

        switch (strtolower(substr(strrchr($filename, '.'), 1))) {
            case 'csv':
                $mime = 'text/csv';
                break;
            case 'pdf':
                $mime = 'application/pdf';
                break;
            case 'zip':
                $mime = 'application/zip';
                break;
            case 'jpeg':
            case 'jpg':
                $mime = 'image/jpg';
                break;
            default:
                $mime = 'application/force-download';
        }

        header('Pragma: public');   // required
        header('Expires: 0'); // no cache
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        //chiquitto
        if ($typeContent == 'file') {
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($content)) . ' GMT');
            header('Content-Length: ' . filesize($content));  // provide file size
        }
        header('Cache-Control: private', false);
        header('Content-Type: ' . $mime);
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: close');

        if ($typeContent == 'file') {
            readfile($content . '/' . $filename); // push it out
        } else {
            echo $content;
        }

        exit();
    }

}
